﻿

namespace Mulala.Cavan.Presentation.Mvc.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Domain.MainBoundedContext.InformationModule;
    using Spring.Context;
    using Spring.Context.Support;
    using Mulala.Cavan.Application.MainBoundedContext.MulalaModule;
    using Mulala.Cavan.Application.MainBoundedContext.AttachmentModule;
    using Mulala.Cavan.Presentation.Seedwork;
    using Mulala.Cavan.Domain.MainBoundedContext.SecurityModule;
    using Mulala.Cavan.Domain.MainBoundedContext.ProprietorModule;
    using Mulala.Cavan.Domain.MainBoundedContext.NannyModule;
    using Mulala.Cavan.Domain.MainBoundedContext.CorporationModule;

    public class NoteViewModelConverter
    {
        public static IList<INoteViewModel> Convert(IList<Note> notes)
        {
            IApplicationContext springContext = ContextRegistry.GetContext();
            IAttachmentAppService attachmentAppService = springContext.GetObject(MulalaAppServiceConst.ATTACHMENTAPPSERVICE) as IAttachmentAppService;

            IList<INoteViewModel> noteViewModels = new List<INoteViewModel>();
            foreach (Note n in notes)
            {
                INoteViewModel noteViewModel = ModelFactory.CreateModel<INoteViewModel>();
                noteViewModel.Item = n;
                if ((SecurityIdentifier.Role)n.SenderUserAccount.Roles[0].ID == SecurityIdentifier.Role.Proprietor)
                {
                    noteViewModel.Avatar = attachmentAppService.GetProprietorAvatar(ProprietorFactory.CreateProprietor(n.SenderUserAccount.ID));
                }
                else if ((SecurityIdentifier.Role)n.SenderUserAccount.Roles[0].ID == SecurityIdentifier.Role.Nanny)
                {
                    noteViewModel.Avatar = attachmentAppService.GetNannyAvatar(NannyFactory.CreateNanny(n.SenderUserAccount.ID));
                }
                else if ((SecurityIdentifier.Role)n.SenderUserAccount.Roles[0].ID == SecurityIdentifier.Role.Corporation)
                {
                    noteViewModel.Avatar = attachmentAppService.GetCorporationLogo(CorporationFactory.CreateCorporation(n.SenderUserAccount.ID));
                }
                noteViewModels.Add(noteViewModel);
            }

            return noteViewModels;
        }
    }
}
